package com.ggb.reactive.task

import com.ggb.reactive.extension.log
import com.ggb.reactive.handler.WebSocketHandler
import lombok.RequiredArgsConstructor
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import java.time.Instant
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@Component
@RequiredArgsConstructor
class WebSocketTask (val webSocketHandler: WebSocketHandler){
    /**
     * 通过WebSocket每隔5秒向客户端发送消息
     */
     //@Scheduled(cron = "0/5 * * * * ?")
    fun sendMessageToClient() {
        log.info("通过WebSocket每隔5秒向客户端发送消息：{}", Instant.now());
        webSocketHandler.sendToAllClient("这是来自服务端的消息：" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
    }
}